# encoding: utf-8
# @File   : main_menu.py
# @Author : 乖孩子
# @Date   : 2025/07/24 16:30:21

import tkinter as tk
from tkinter import ttk
import os
from ui.login_frame import LoginFrame
from ui.creator_frame import PointCreatorFrame
from ui.theme import Theme


class MainMenu(tk.Frame):
    def __init__(self, parent, logger, log_collector):
        super().__init__(parent, bg=Theme.COLORS["background"])
        self.parent = parent
        self.logger = logger
        self.log_collector = log_collector

        self.parent.title("智能测点管理系统")
        self.parent.geometry("900x600")
        self.parent.minsize(800, 500)

        # 设置主题
        Theme.setup_theme(self.parent)

        # 创建UI
        self.create_widgets()
        self.pack(fill=tk.BOTH, expand=True)

        logger.info("主菜单初始化完成")

    def create_widgets(self):
        """创建主界面组件"""
        # 标题区域
        title_frame = ttk.Frame(self, style="TFrame")
        title_frame.pack(pady=50)

        ttk.Label(
            title_frame,
            text="智能测点管理系统",
            font=Theme.FONTS["title"],
            foreground=Theme.COLORS["primary"]
        ).pack(pady=10)

        # 功能按钮区域
        button_frame = ttk.Frame(self, style="TFrame")
        button_frame.pack(pady=50)

        # 登录按钮
        login_btn = ttk.Button(
            button_frame,
            text="系统登录",
            command=self.open_login,
            style="Accent.TButton"
        )
        login_btn.pack(pady=15, ipadx=20, ipady=5)
        self.add_button_hover_effect(login_btn)

        # 测点创建按钮
        create_btn = ttk.Button(
            button_frame,
            text="测点创建",
            command=self.open_point_creator
        )
        create_btn.pack(pady=15, ipadx=20, ipady=5)
        self.add_button_hover_effect(create_btn)

        # 底部信息
        footer = ttk.Label(
            self,
            text="© 2025 智能测点管理系统 | 版本 v1.0.0",
            font=Theme.FONTS["small"],
            foreground=Theme.COLORS["text_light"],
            background=Theme.COLORS["background"]
        )
        footer.pack(side=tk.BOTTOM, pady=20)

    def add_button_hover_effect(self, button):
        """为按钮添加悬停效果"""

        def on_enter(e):
            button.configure(style="Accent.TButton")

        def on_leave(e):
            if button["text"] == "系统登录":
                button.configure(style="Accent.TButton")
            else:
                button.configure(style="TButton")

        button.bind("<Enter>", on_enter)
        button.bind("<Leave>", on_leave)

    def open_login(self):
        """打开登录界面"""
        self.logger.info("切换到登录界面")
        self.pack_forget()
        login_frame = LoginFrame(self.parent, self.show_main_menu, self.logger, self.log_collector)
        login_frame.pack(fill=tk.BOTH, expand=True)

    def open_point_creator(self):
        """打开测点创建界面"""
        self.logger.info("切换到测点创建界面")
        self.pack_forget()
        creator_frame = PointCreatorFrame(self.parent, self.show_main_menu, self.logger, self.log_collector)
        creator_frame.pack(fill=tk.BOTH, expand=True)

    def show_main_menu(self):
        """显示主菜单"""
        self.logger.info("返回主菜单")
        # 清除当前界面
        for widget in self.parent.winfo_children():
            widget.pack_forget()

        # 显示主菜单
        self.pack(fill=tk.BOTH, expand=True)